var FacilityLevelForm = function () {
  var FacilityLevelModel = function (data) {
    BaseModel.call(this);

    var requiredFields = ['rank', 'difficultLevelRate', 'status'];
    var self = this;
    var fn = {
      initData: function (data) {
        if (data === undefined) {
          data = {
            status: 1,
            rank: '',
            difficultLevelRate: undefined
          };
        } else {
          self.id = data.id;
        }

        var fields = requiredFields;
        for (var i = 0; i < fields.length; i++) {
          var field = fields[i];
          var value = data[field];
          self[field] = value;
        }
      }
    };

    this.checkValid = function(){
      var fields = requiredFields;
      for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        var value = this[field];
        if (SysUtil.isEmpty(value)) {
          return field;
        }
      }
      return '';
    };

    this.getObject = function(){
      var fields = ['rank', 'status'];
      var postData = {};
      this.setPostData(postData, fields, false);

      var difficultLevelRate = this.difficultLevelRate;
      if (difficultLevelRate === undefined) {
        difficultLevelRate = 0;
      } else {
        difficultLevelRate = _.round(difficultLevelRate / 100, 3);
      }
      postData.difficultLevelRate = difficultLevelRate;

      var id = this.id;
      if (!SysUtil.isEmpty(id)) {
        postData.id = id;
      }

      return postData;
    };

    fn.initData(data);
  };

  var template = /* html */ `
    <div class="facility-level-form" v-loading.fullscreen.lock="loading">
      <el-form label-width="auto">
        <el-form-item label="等级">
          <el-input v-model="levelForm.rank" placeholder="请输入等级" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="难易度">
          <el-input-number is-decimal-zero v-model="levelForm.difficultLevelRate" :controls="false" :min="0" :precision="1"
            placeholder="请输入难易度百分比" style="width:300px">
          </el-input-number>
          <span>%</span>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="levelForm.status" style="width:300px" placeholder="请选择状态">
            <el-option v-for="item in statusList" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div style="text-align:center">
        <el-button type="primary" @click="ensure">
          <span v-if="type==='add'">新增</span>
          <span v-else>修改</span>
        </el-button>
      </div>
    </div>
  `;

  return {
    props: {
      level: {
        type: Object,
        default: function () {
          return null;
        }
      },
      type: {
        type: String,
        default: 'add'
      }
    },

    data: function () {
      var levelForm;
      if (this.type === 'add') {
        levelForm = new FacilityLevelModel();
      } else {
        levelForm = new FacilityLevelModel(this.level);
      }
      return {
        loading: false,
        levelForm: levelForm,
        statusList: i18n.getDropDown('bomBusiness.status', [1, 0])
      };
    },

    methods:{
      ensure:function(){
        var levelForm = this.levelForm;
        var code = levelForm.checkValid();
        if (code !== '') {
          var msg = i18n.getMsg(`bomBusiness.facilityLevelSetting.error.${code}`);
          this.$alert(msg, '信息');
          return;
        }

        if (this.type === 'add') {
          this.toAddLevel(levelForm);
        } else {
          this.toUpdateLevel(levelForm);
        }
      },

      toAddLevel:function(levelForm){
        var postData = levelForm.getObject();
        this.loading = true;
        var self = this;
        ajaxRequest('createOrderForBomOrder/saveDifficultLevelRate', postData).then(function () {
          self.loading = false;
          self.$alert('新增难易系数成功', '信息', {
            callback: function () {
              self.$emit('save-success');
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      },

      toUpdateLevel:function(levelForm){
        var postData = levelForm.getObject();
        this.loading = true;
        var self = this;
        ajaxRequest('createOrderForBomOrder/updateDifficultLevelRate', postData).then(function () {
          self.loading = false;
          self.$alert('修改难易系数成功', '信息', {
            callback: function () {
              self.$emit('save-success');
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    template: template
  };
}();
